﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ToolsCXml;

namespace JHStock.UserForm
{
    public partial class FormShowStockImage : Form
    {
        public UpdateMonitInfors _umi;
        private JHConfig _jhconfig;
        private string _kdatatype;
        private List<Stock> _list;
        private BEId _ActiveBE;
        private DataTable _dt;
        private Stocks stocks { get { return _jhconfig.Stocks; } }
        private StocksData ActiveStocksData { get { _jhconfig.SetKDataType(_kdatatype); return _jhconfig.ActiveStocksData; } }
        public FormShowStockImage(JHConfig jhconfig, string kdatatype, List<Stock> list)
        {
            InitializeComponent();
            this._jhconfig = jhconfig;
            this._kdatatype = kdatatype;
            this._list = list;
            _ActiveBE = null;
            _umi = new UpdateMonitInfors();
        }
        private void FormShowStockImage_Load(object sender, EventArgs e)
        {
            SetComboxMA();
            int B = 0;
            int E = 0;
            List<BEId> bes = new List<BEId>();
            while (B < _list.Count)
            {
                E += 10;
                if (E < _list.Count)
                {
                    bes.Add(new BEId(B, E));
                    B = E;
                }
                else
                {
                    bes.Add(new BEId(B, _list.Count));
                    break;
                }
            }
            listBox1.Items.AddRange(bes.ToArray());
                
            _dt = TestStocks.CreateShowStockDataTable();
            dataGridView1.DataSource = _dt;
            TestStocks.InitShowStockDgvUI(_dt,dataGridView1);
        }
        private void SetComboxMA()
        {
            for(int i=0; i<comboBoxMA.Items.Count; i++)
                if (_kdatatype == comboBoxMA.Items[i].ToString().ToLower())
                {
                    comboBoxMA.SelectedIndex = i;
                    return;
                }
        }
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex == -1) return;
            _ActiveBE = (BEId)listBox1.SelectedItem;
            ShowItems();
        }
        private void comboBoxMA_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBoxMA.SelectedIndex == -1) return;
            _kdatatype = comboBoxMA.SelectedItem.ToString().ToLower();
            ShowItems();
        }

        private void ShowItems()
        {
            if (_ActiveBE == null) return;
            _ActiveBE.MoveToTop();
            _dt.Rows.Clear();
            _umi.Clear();
            while (_ActiveBE.HasNext)
            {
                Stock s = _list[ _ActiveBE.ActiveIndex];
                DataRow dr = _dt.NewRow();
                dr["ID"] = s.ID;
                dr["名称"] = s.Name;
                //if (e.KeyCode == Keys.O)
                //    cs = ColorStyles.print;
                //Bitmap bmp = StockDraw.DrawDailyLocal(s,ActiveStocksData, cs);
                //dr["画图"] = bmp;
                _umi.Add(s, dr);
                _dt.Rows.Add(dr);
                _ActiveBE.MoveNext();
            }
            ColorStyle cs = ColorStyles.classic;
            _umi.run(ActiveStocksData, cs);
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridView  dgv= dataGridView1;
            if (e.RowIndex != -1 && e.ColumnIndex != -1)
            {
                Bitmap bmp = (Bitmap)dgv[e.ColumnIndex, e.RowIndex].Value;
                if (bmp != null)
                {
                    if (checkBoxZoom.Checked)
                    {
                        FormShowPicture f = new FormShowPicture(bmp);
                        f.ShowDialog();
                    }
                    else
                    {
                        FormPictureBox f = new FormPictureBox(bmp);
                        f.ShowDialog();
                    }
                }
                return;
                string numcode = dgv[1, e.RowIndex].Value.ToString().Substring(2, 6);
                Stock s = _jhconfig.Stocks.StockByNumCode(numcode);
                if (s != null && s.Bmp != null)
                {//showImg
                    if (e.ColumnIndex == 2)
                    {
                        {
                            FormPictureBox f = new FormPictureBox(s.Bmp);
                            f.ShowDialog();
                        }
                    }
                    else if (e.ColumnIndex == 1)
                    {
                        string type = s.Tag.GetType().ToString();
                        FormPictureBox f = new FormPictureBox((Bitmap)s.Tag);
                        f.ShowDialog();

                    }
                }
            }
        }

       
    }
}
